package config

import (
	"encoding/json"
	"os"
	"path/filepath"

	"cashway.com/cis/assistant/types"
	"github.com/sirupsen/logrus"
)

var Config = types.ConfigType{
	Port:                  8520,
	Domain:                "127.0.0.1",
	LogsPath:              filepath.Join(getExecutePath(), "./logs"),
	LogLinkName:           "assistant.log",
	LogNamePattern:        "%Y%m%d.log",
	LogSaveMaxAge:         30 * 24 * 60 * 60,
	RotationTime:          24 * 60 * 60,
	RotationSize:          20 * 1024 * 1024,
	EnableParamsLog:       true,
	EnableJsonRespBodyLog: true,
}

func init() {
	file, err := os.Open(filepath.Join(getExecutePath(), "assistant.conf.json"))
	if err != nil {
		logrus.Info("在当前路径下未找到\"assistant.conf.json\"配置文件，使用默认配置")
		logrus.Info(Config)
		return
	}
	decoder := json.NewDecoder(file)
	decodeErr := decoder.Decode(&Config)
	if decodeErr != nil {
		logrus.Error("\"assistant.conf.json\" 文件格式错误，请检查", err)
	}
	logrus.Info(Config)
}

// 获取执行程序所在路径
func getExecutePath() string {
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		logrus.Error("获取当前路径失败", err)
		return "./"
	}
	return dir
}
